package com.nineclock.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.nineclock.system.service.SysCompanyUserService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 该listener不能交给Spring容器管理 , 如果我们需要在 Listener中用到某一个Spring容器的bean , 通过构造参数传递
 */
@Slf4j
public class ExcelMemberParserListener extends AnalysisEventListener<ExcelMember> {

    private List<ExcelMember> memberList = new ArrayList<>();

    private SysCompanyUserService companyUserService;
    public ExcelMemberParserListener() {
    }
    public ExcelMemberParserListener(SysCompanyUserService companyUserService){
        this.companyUserService = companyUserService;
    }

    /**
     * 解析每一行时都会被调用
     * @param data
     * @param context
     */
    @Override
    public void invoke(ExcelMember data, AnalysisContext context) {
        log.info("-----------> 正在每一行的数据, 数据内容: " + data);
        memberList.add(data);
    }

    /**
     * 整个Excel的表格解析完毕后调用
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("-----------> [End] : 整个表格解析完毕 .....");
        companyUserService.importData(memberList);
    }

}
